package com.cxyd.rest;



import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cxyd.common.Constant;
import com.cxyd.common.PageResult;
import com.cxyd.common.QueryPageBean;
import com.cxyd.common.RestResponse;
import com.cxyd.domain.Stu;
import com.cxyd.service.StuService;
import io.swagger.annotations.*;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: huanglianyun
 * @Description:
 * @DATE: Created in 2020/2/4 16:28.
 */
@RestController
@RequestMapping("/stu")
@Log4j2
@Api(value = "学生学籍信息管理接口", description = "提供学生学籍信息增删改查")
public class StuRest {

    @Autowired
    private StuService stuService;

    @PostMapping(value = "/save")
    @ApiOperation("学生信息新增接口")
    @ApiImplicitParams({//多个请求参数
            @ApiImplicitParam(name="stu",value = "学生信息实体类",dataType = "Stu")
    })
    public RestResponse save(@RequestBody Stu stu){
        try{
            stuService.addStu(stu);
        }catch (Exception e){
            e.printStackTrace();
            return RestResponse.error(Constant.REST_RESP_error_MSG);
        }
        return RestResponse.ok(Constant.REST_RESP_OK_MSG);
    }

    //分页查询
    @ApiOperation("分页查询学生信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name="queryPageBean",value = "分页查询bean",dataType = "QueryPageBean"),//一个请求参数
            @ApiImplicitParam(name = "name", value = "学生姓名,模糊查询",  dataType = "String"),
            @ApiImplicitParam(name = "teacher", value = "教师,模糊查询",  dataType = "String"),
            @ApiImplicitParam(name = "course", value = "课程类型,模糊查询",  dataType = "String")
    })
    @PostMapping(value = "/findPage")
    public RestResponse getPrjUser(@RequestBody QueryPageBean queryPageBean,
                                   @RequestParam(required = false)  String name,
                                   @RequestParam(required = false)  String teacher,
                                   @RequestParam(required = false) String course) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        //String queryString = queryPageBean.getQueryString();//查询条件

        Long total = null;
        List<Stu> rows = null;
        PageResult pageResult = null;
        try{
            Page<Stu> page = new Page<>(currentPage == null? 1 : currentPage, pageSize == null ? 5 : pageSize);

            IPage<Stu> stuIPage = stuService.selectPageStu(page,name,teacher,course);
            total = stuIPage.getTotal();
            rows = stuIPage.getRecords();

            pageResult = new PageResult(total, rows);
        }catch (Exception e){
            e.printStackTrace();
            return RestResponse.error(Constant.REST_RESP_error_MSG);
        }
        return RestResponse.ok(pageResult);
    }


}
